package rx1;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

public class Hello1 {
	
	//被观察者，事件源
		@SuppressWarnings("deprecation")
		Observable<String> myObservable0 = Observable.create(  
			    new Observable.OnSubscribe<String>() {  
					@Override
					public void call(Subscriber<? super String> t) {
						t.onNext("Hello, world1!");  
			            t.onCompleted();  
					}  
			    }  
			);  
		
		//观察者
		Subscriber<String> mySubscriber0 = new Subscriber<String>() {  
		    @Override  
		    public void onNext(String s) { System.out.println(s); }  
		  
		    @Override  
		    public void onCompleted() { }  
		  
		    @Override  
		    public void onError(Throwable e) { }  
		};
		/**
		 * 基本用法
		 */
		public void hello0() {
			myObservable.subscribe(onNextAction); 
		}	
		
	Observable<String> myObservable = Observable.just("Hello, world2!"); 
	
	Action1<String> onNextAction = new Action1<String>() {  
	    @Override  
	    public void call(String s) {  
	        System.out.println(s);  
	    }  
	}; 
	/**
	 * 基本简写
	 */
	public void hello() {
		myObservable.subscribe(onNextAction); 
	}
	/**
	 * RxJava简写
	 */
	public void hello2() {
		Observable.just("Hello, world_2_简写!")  
	    .subscribe(new Action1<String>() {  
	        @Override  
	        public void call(String s) {  
	              System.out.println(s);  
	        }  
	    });  
	}
	/**
	 * 使用lambda简写
	 */
	public void hello3() {
		Observable.just("Hello, world_2_lambda!")  
	    .subscribe(s -> System.out.println(s)); 
	}
	/**
	 * map操作符，用来把把一个事件转换为另一个事件的。
	 */
	public void hello4() {
		Observable.just("Hello, world!")  
		  .map(new Func1<String, String>() {  
		      @Override  
		      public String call(String s) {  
		          return s + " -Dan";  
		      }  
		  }).subscribe(new Action1<String>() {

			@Override
			public void call(String t) {
				// TODO Auto-generated method stub
				System.out.println(t);
			}
		});
	}
	/**
	 * map操作符，用来把把一个事件转换为另一个事件的。
	 * lambda简化
	 */
	public void hello4_s() {
		Observable.just("Hello, world!")  
	    .map(s -> s + " -Dan")  
	    .subscribe(s -> System.out.println(s));  
	}
	
	public void hello5() {
		Observable.just("Hello, world!")  
	    .map(new Func1<String, Integer>() {  
	        @Override  
	        public Integer call(String s) {  
	            return s.hashCode();  
	        }  
	    })  
	    .subscribe(i -> System.out.println(Integer.toString(i))); 
		
		System.out.println("==========");
		
		Observable.just("Hello, world!")  
	    .map(s -> s.hashCode())  
	    .subscribe(i -> System.out.println(Integer.toString(i)));  
		
		System.out.println("=====================");
		
		Observable.just("Hello, world!")  
	    .map(s -> s.hashCode())  
	    .map(i -> Integer.toString(i))  
	    .subscribe(s -> System.out.println(s)); 
	}
	
	public void hello6() {
		  
	}
	
	
	
}
